const intervals = [
  [1, 4],
  [0, 4],
];
var merge = function (intervals) {
  intervals.sort((item1, item2) => item1[0] - item2[0]);
  console.log(intervals);
  let array = [];
  intervals.forEach((element) => {
    if (array.length == 0) {
      array.push(element);
    }
    let item = array.pop();
    console.log(item,'item',array,'array');
    if (item[1] >= element[0]) {
      let targetItem = [];
      targetItem.push(item[0]);
      targetItem.push(item[1] > element[1] ? item[1] : element[1]);
      array.push(targetItem);
    } else {
      array.push(item);
      array.push(element);
    }
  });
  return array;
};

console.log(merge(intervals));
